package com.dongjiantong.farmrecords.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.utils.ReportNumberMakerUtil;
import com.dongjiantong.common.web.sso.LoginInfo;
import com.dongjiantong.farmrecords.enums.FarmRecordsNumberStatus;
import com.dongjiantong.farmrecords.mapper.FarmRecordsPollutionMapper;
import com.dongjiantong.farmrecords.model.FarmRecordsPollution;
import com.dongjiantong.farmrecords.service.FarmRecordsPollutionService;
import com.dongjiantong.mybatis.ServiceMybatis;
import com.dongjiantong.user.enums.FarmsEnum;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 治污中心-治污记录
 */
@Slf4j
@Service(version = "1.0.0")
public class FarmRecordsPollutionServiceImpl extends ServiceMybatis<FarmRecordsPollution> implements FarmRecordsPollutionService {

    @Resource
    private FarmRecordsPollutionMapper farmRecordsPollutionMapper;

    /**
     * 根据条件查询
     * @param pageNum
     * @param pageSize
     * @param map   查询条件
     * @return  PageInfo<FarmRecordsPollution>
     */
    @Override
    public PageInfo<FarmRecordsPollution> selectByMap(Integer pageNum, Integer pageSize, Map map) {
        try {
            if (farmRecordsPollutionMapper == null){
                throw new OPServerException("farmRecordsPollutionMapper 为null,请检查");
            }
            PageHelper.startPage(pageNum,pageSize);
            //查询有效单据
            map.put("delete_flag", FarmRecordsNumberStatus.EFFECTIVE.getCode());
            //查询已启用的养殖场
            map.put("farmStatus", FarmsEnum.status_true.getCode());
            List<FarmRecordsPollution> farmRecordsPollutionList = farmRecordsPollutionMapper.selectByMap(map);
            return new PageInfo<FarmRecordsPollution>(farmRecordsPollutionList);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.INTERNAL_SERVER_ERROR,HttpCode.INTERNAL_SERVER_ERROR.getMessage());
        }
    }

    /**
     * 新建一条记录 (暂存状态下)
     * @param farmRecordsPollution
     * @param toStatus
     * @return  long 返回插入数据的id
     */
    @Override
    public Long createOne(LoginInfo loginInfo,FarmRecordsPollution farmRecordsPollution, Integer toStatus) {
        int id = 0;
        try {
            if (farmRecordsPollutionMapper == null){
                throw new OPServerException("farmRecordsPollutionMapper 为null,请检查");
            }
            Integer fromStatus = farmRecordsPollution.getStatus();

            //todo 生成单据号
            //单据生成规则暂未定(此处为测试)
            String prefix = "ZZW";
            prefix = ReportNumberMakerUtil.createPrefix(prefix,loginInfo.getProvinceId(),loginInfo.getCityId(),loginInfo.getRegionId());
            String maxNumber = farmRecordsPollutionMapper.findMaxNumber(prefix);
            String number = ReportNumberMakerUtil.create(prefix,maxNumber);
            farmRecordsPollution.setNumber(number);

            farmRecordsPollution.setProvince_id(loginInfo.getProvinceId());
            farmRecordsPollution.setCity_id(loginInfo.getCityId());
            farmRecordsPollution.setRegion_id(loginInfo.getRegionId());
            farmRecordsPollution.setStreet_id(loginInfo.getStreetId());
            farmRecordsPollution.setVillage_id(loginInfo.getVillageId());

            farmRecordsPollution.setDelete_flag(FarmRecordsNumberStatus.EFFECTIVE.getCode());

            //todo 单据从状态到状态
           if (fromStatus == 0 && toStatus == null) {
               //直接暂存
               id = farmRecordsPollutionMapper.createOne(farmRecordsPollution);
           }else if (!fromStatus.equals(toStatus) && FarmRecordsNumberStatus.SUBMIT.getCode().equals(toStatus)) {
               //直接提交
               farmRecordsPollution.setStatus(toStatus);
               id = farmRecordsPollutionMapper.createOne(farmRecordsPollution);
           }
            //todo 生成政府操作记录

            return Long.valueOf(farmRecordsPollution.getId());
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.DATA_CREATE_FAIL.getCode(),HttpCode.DATA_CREATE_FAIL.getMessage());
        }
    }

    /**
     * 更新一条记录 (根据id或者number)
     * @param farmRecordsPollution
     * @return  boolean (更新成功返回true 反之返回false)
     */
    @Override
    public Integer updateByIdOrNumber(FarmRecordsPollution farmRecordsPollution,Integer toStatus) {
        try {
            if (farmRecordsPollutionMapper == null){
                throw new OPServerException("farmRecordsPollutionMapper 为null,请检查");
            }
            farmRecordsPollution.setStatus(toStatus);
            farmRecordsPollution.setUpdated_at(new Date());
            Integer con = farmRecordsPollutionMapper.updateByPrimaryKeySelective(farmRecordsPollution);
            return con;
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.DATA_UPDATE_FAIL,HttpCode.DATA_UPDATE_FAIL.getMessage());
        }
    }

    @Override
    public PageInfo<FarmRecordsPollution> selectListByStatus(Integer pageNum, Integer pageSize, Integer status, Long farmId) {
        PageHelper.startPage(pageNum,pageSize);

        Map<String,Object> map = Maps.newHashMap();

        if (status != null) {
            map.put("status",status);
        }

        map.put("farm_id",farmId);
        map.put("delete_flag",FarmRecordsNumberStatus.EFFECTIVE.getCode());

        List<FarmRecordsPollution> farmRecordsPollutionList = farmRecordsPollutionMapper.selectListByStatus(map);

        return new PageInfo<FarmRecordsPollution>(farmRecordsPollutionList);
    }

    /**
     * 养殖端 治污分析
     */@Override
    public Map<String,Object> pollutionAnalysis(Long farmId) {
        Map<String,Object> map = Maps.newHashMap();

        map.put("delete_flag",FarmRecordsNumberStatus.EFFECTIVE.getCode());
        map.put("farm_id",farmId);
        map.put("status",FarmRecordsNumberStatus.SUBMIT.getCode());

        Map<String,Object> result = farmRecordsPollutionMapper.pollutionAnalysis(map);

        return result;
    }

    /**
     * (政府端)治污分析
     * @param loginInfo
     * @param farmId
     * @param breedSecondType
     * @return
     */
    @Override
    public Map<String,Object> pollutionAnalysisGov(Date startDate, Date endDate,LoginInfo loginInfo, Long farmId, Integer breedSecondType) {
        Long userId = loginInfo.getId();

        Map<String,Object> map = Maps.newHashMap();

        if (startDate != null) {
            map.put("startDate",startDate);
        }
        if (endDate != null) {
            map.put("endDate",endDate);
        }

        map.put("delete_flag",FarmRecordsNumberStatus.EFFECTIVE.getCode());
        map.put("userId",userId);
        map.put("farm_id",farmId);
        map.put("status",FarmRecordsNumberStatus.SUBMIT.getCode());
        if (breedSecondType != null) {
            map.put("breed_second_type",breedSecondType);
        }

        Map<String,Object> result = farmRecordsPollutionMapper.pollutionAnalysisGov(map);

        return result;
    }

}
